<?php
/**
 * User MyReservations edit Controller
 *
 * @access 	public
 * @author 	Valentin
 * @copyright	ResMania 2009 all rights reserved.
 * @link		http://developer.resmania.com/api
 */
class RM_User_MyReservations_PlannerController extends RM_User_MyReservations_MainController
{
    /**
     * gets the calendar details (names, colors etc).
     *
     * @return JSON
     */
    public function getcalendarsJsonAction() {

        // get all units
        $unitModel = new RM_Units();
        $units = $unitModel->getAll(new RM_Unit_Search_Criteria());
        $calArray = array();

        foreach ($units as $unit) {
            $calArray[] = array(
                "id" => $unit->id,
                "title" => $unit->name,
                "color" => $unit->id
            );
        }
        return array(
            'data' => array("calendars" => $calArray)
        );
    }


    /**
     * Gets the calendar Data
     *
     * @return JSON
     */
    public function getdataJsonAction() {

        $start = $this->_getParam('start');
        $end = $this->_getParam('end');

        if (!$startRange || !$endRange) {
            $startRange = date("Y-m-d");
            $endRange = date("Y-m-d");
        }

        $unitModel = new RM_Units();
        $units = $unitModel->getAll(new RM_Unit_Search_Criteria());

        $unitModel = new RM_Units();
        $reservationModel = new RM_Reservations();
        $config = new RM_Config();
        $evtsArray = array();

        // add 6 months to either side of the reservation
        $RMDate = new RM_Date();
        $start = $RMDate->dateSub($startRange, 180);
        $end = $RMDate->dateAdd($endRange, 180);

        $filter = array();
        $filter['data']['type'] = 'date';
        $filter['field'] = 'start_datetime';
        $filter['data']['comparison'] = 'gt';
        $filter['data']['value'] = $start;
        $filterArray[] = $filter;

        $filter = array();
        $filter['data']['type'] = 'date';
        $filter['field'] = 'end_datetime';
        $filter['data']['comparison'] = 'lt';
        $filter['data']['value'] = $end;
        $filterArray[] = $filter;

        $filter = array();
        $filter['data']['type'] = 'numeric';
        $filter['field'] = 'in_progress';
        $filter['data']['comparison'] = 'eq';
        $filter['data']['value'] = 0;
        $filterArray[] = $filter;

        $user = $this->getUser();

        foreach ($units as $unit) {
            $unit = $unitModel->get($unit->id);
            $reservations = $reservationModel->fetchAllByUserID($user->id);
            foreach ($reservations as $period) {

                $idArray = explode("-", $period->id);
                $id = (int) $idArray[1];

                // check time
                $ad = true; // all day
                $starttime = explode(" ",$period->start_datetime);
                if ($starttime[1] !== "00:00:00"){ $ad = false; }
                $endtime = explode(" ",$period->end_datetime);
                if ($endtime[1] !== "23:30:00"){ $ad = false; }

                $evtsArray[] = array(
                    "id" => $id,
                    "cid" => $unit->id,
                    "uid" => (int) $period->user_id,
                    "title" => "",
                    "start" => $period->start_datetime,
                    "end" => $period->end_datetime,
                    "ad" => $ad
                );
            }
        }

        return array(
            'data' => array(
                "success" => true,
                "message" => RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_MAIN)->_('Admin.Reservation.Edit', "LoadedData"),
                "data" => $evtsArray
            )
        );
    }
}